package com.miao.hong.fang.util.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.miao.hong.fang.user.UserTO;


public class JMSUtil {

	public static void sendMsg(UserTO user)  {
		
		try {
			InitialContext initCtx = new InitialContext();
			Context envContext = (Context) initCtx.lookup("java:comp/env");  
			ConnectionFactory factory = (ConnectionFactory) envContext.lookup("jms/connectionFactory");
			Connection conn = factory.createConnection();
			conn.start();
			
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue register_queue = (Queue) envContext.lookup("jms/register_queue");
			MessageProducer producer = session.createProducer(register_queue);
			
			ObjectMessage msg = session.createObjectMessage();
			msg.setObject(user);
			
			producer.send(msg);
			
		} catch (Exception e) {
			
			System.out.println("Error occured while sending message");
			e.printStackTrace();
		}
	}
	
}
